/*
 * @Author: 江舟 jijiangzhou@recycloud.cn
 * @Date: 2024-03-19 14:53:12
 * @LastEditors: 江舟 jijiangzhou@recycloud.cn
 * @LastEditTime: 2024-05-16 14:52:25
 * @FilePath: \vite-project\src\store\user.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import { create } from 'zustand';
import { persist } from 'zustand/middleware';
import { TOKEN_NAME } from 'constant';

interface State {
    menus: any[];
    menuMap: any;
    menuPaths: string[];
    firstPath: string | null;
    userInfo: API.LoginUserInfoResponseType | null;
    pageTabs: any[];
    subMenuPath: string[];
    actionPerms: string[];
}

interface Action {
    updateMenu: (
        menus: any[],
        menuMap: any,
        menuPaths: string[],
        firstPath?: string,
        subMenuPath?: string[],
        actionPerms?: string[],
    ) => void;
    updateUserInfo: (user: API.LoginUserInfoResponseType) => void;
    updatePageTabs: (user: any) => void;
}

const useUserStore = create<State & Action>((set) => ({
    menus: [],
    menuMap: {},
    menuPaths: [],
    subMenuPath: [],
    firstPath: null,
    userInfo: null,
    pageTabs: [],
    actionPerms: [],
    updateUserInfo: (user: API.LoginUserInfoResponseType) => {
        set((state) => ({
            userInfo: {
                id: user.id,
                restPasswdTag: user.restPasswdTag,
                userName: user.userName,
                name: user.name,
                mobile: user.mobile,
                sex: user.sex,
                tenantCount: user.tenantCount || 0,
                tenants: user.tenants,
                roles: user.roles,
            },
        }));
    },
    updateMenu: (menus, menuMap, menuPaths, firstPath, subMenuPath, actionPerms) => {
        set((state) => ({
            menus,
            name: '闲闲',
            menuMap,
            menuPaths,
            firstPath,
            subMenuPath,
            actionPerms,
        }));
    },
    updatePageTabs: (tabs) => {
        set((state) => ({
            pageTabs: tabs,
        }));
    },
}));

// token 持久化
export const useTokenStore = create<
    {
        claerToken(): unknown;
        currentTenant: string | null;
        token: string | null;
        expireAt: number | null;
        setExpireAt: (expireAt: number) => void;
        setToken: (token: string) => void;
        setCurrentTenant: (tenant: string) => void;
    },
    [['zustand/persist', any]]
>(
    persist(
        (set, get) => ({
            currentTenant: null,
            token: null,
            expireAt: null,
            setToken: (token: string) => set({ token }),
            setExpireAt: (expireAt: number) => set({ expireAt }),
            setCurrentTenant: (tenant: string) => set({ currentTenant: tenant }),
            claerToken: () =>
                set({
                    token: null,
                    expireAt: null,
                    currentTenant: null,
                }),
        }),
        {
            name: TOKEN_NAME, // 唯一键
            getStorage: () => localStorage, // (可选)默认使用'localStorage'
        },
    ),
);

export default useUserStore;
